package net.moon.factory;

import net.moon.method.IPayMethod;

import java.io.IOException;
import java.util.Properties;

/**
 * @author moonlan
 * date 2021/3/20 上午10:45
 */
@SuppressWarnings(value = "unused")
public class PayMethodFactory {

    private final Properties properties;

    private PayMethodFactory(Properties properties) {
        this.properties = properties;
    }

    public static PayMethodFactory getInstance() {
        Properties properties = new Properties();
        try {
            properties.load(PayMethodFactory.class.getClassLoader().getResourceAsStream("pro.properties"));
            return new PayMethodFactory(properties);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public IPayMethod getPayMethodInstance() {
        try {
            Class<?> method = Class.forName(properties.getProperty("pay.method"));
            Object instance = method.getConstructor().newInstance();
            return (IPayMethod) instance;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

}
